package com.yxzr.youyou;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yxzr.youyou.adapter.ComicRecyclerViewAdapter;
import com.yxzr.youyou.adapter.ComicTypeAdapter;
import com.yxzr.youyou.base.BaseActivity;
import com.yxzr.youyou.bean.TwoTypeResBean;
import com.yxzr.youyou.bean.TypeBean;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.RecyclerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class HomeTypeActivity extends BaseActivity {
    private TextView mHomeTypeTitle;
    private RecyclerView mComicType,mComicAddress,mComicYear,mComicLanuage,mComicMovie;
    private ComicTypeAdapter adapter1,adapter2,adapter3,adapter4;
    private ComicRecyclerViewAdapter adapter;
    private RadioGroup comic_rg;
    private List<TypeBean.DataBeanX.DataBean> typeList = new ArrayList<>();
    private List<TypeBean.DataBeanX.DataBean> addressList = new ArrayList<>();
    private List<TypeBean.DataBeanX.DataBean> yearList = new ArrayList<>();
    private List<TypeBean.DataBeanX.DataBean> lanuageList = new ArrayList<>();
    private int typeIndex = 0,addressIndex = 0,yearIndex = 0,lanuageIndex = 0;
    private int index = 2;
    private String pid = "";
    private String type = "";
    private String vipString = "";
    private int vipIndex = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hometype);
        pid = getIntent().getStringExtra("pid");
        type = getIntent().getStringExtra("type");
        typeIndex = getIntent().getIntExtra("selectPos",0);
        vipIndex = getIntent().getIntExtra("index",0);
        setTitleWithBack();
        initView();
    }

    private void initView() {
        mHomeTypeTitle = findViewById(R.id.hometype_tv_title);
        mComicType = findViewById(R.id.comic_rv_type);
        mComicAddress = findViewById(R.id.comic_rv_address);
        mComicYear = findViewById(R.id.comic_rv_year);
        mComicLanuage = findViewById(R.id.comic_rv_language);
        mComicMovie = findViewById(R.id.comic_rv_movie);
        comic_rg = findViewById(R.id.comic_rg);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mComicType.addItemDecoration(new RecyclerItemDecoration(10,1));
        mComicType.setLayoutManager(linearLayoutManager);
        adapter1 = new ComicTypeAdapter(this);
        mComicType.setAdapter(adapter1);

        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);
        linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        mComicAddress.addItemDecoration(new RecyclerItemDecoration(10,1));
        mComicAddress.setLayoutManager(linearLayoutManager1);
        adapter2 = new ComicTypeAdapter(this);
        mComicAddress.setAdapter(adapter2);

        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(this);
        linearLayoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        mComicYear.addItemDecoration(new RecyclerItemDecoration(10,1));
        mComicYear.setLayoutManager(linearLayoutManager2);
        adapter3 = new ComicTypeAdapter(this);
        mComicYear.setAdapter(adapter3);

        LinearLayoutManager linearLayoutManager3 = new LinearLayoutManager(this);
        linearLayoutManager3.setOrientation(LinearLayoutManager.HORIZONTAL);
        mComicLanuage.addItemDecoration(new RecyclerItemDecoration(10,1));
        mComicLanuage.setLayoutManager(linearLayoutManager3);
        adapter4 = new ComicTypeAdapter(this);
        mComicLanuage.setAdapter(adapter4);

        GridLayoutManager layoutManager = new GridLayoutManager(this,3);
        mComicMovie.setLayoutManager(layoutManager);
        mComicMovie.addItemDecoration(new RecyclerItemDecoration(10,3));
        mComicMovie.setNestedScrollingEnabled(false);
        adapter = new ComicRecyclerViewAdapter(this,vipIndex);
        mComicMovie.setAdapter(adapter);

        adapter1.setOnClickListener(new ComicTypeAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                typeIndex = position;
                adapter1.setSelectIndex(position);
                initData();
            }
        });

        adapter2.setOnClickListener(new ComicTypeAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                addressIndex = position;
                adapter2.setSelectIndex(position);
                initData();
            }
        });

        adapter3.setOnClickListener(new ComicTypeAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                yearIndex = position;
                adapter3.setSelectIndex(position);
                initData();
            }
        });

        adapter4.setOnClickListener(new ComicTypeAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                lanuageIndex = position;
                adapter4.setSelectIndex(position);
                initData();
            }
        });

        comic_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.comic_rb_one:
                        index = 2;
                        initData();
                        break;
                    case R.id.comic_rb_two:
                        index = 1;
                        initData();
                        break;
                }
            }
        });
        initType();
    }

    private void initType(){
        RequestUtil.getInstence().API().movieClick(type)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(this,true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        TypeBean bean = gson.fromJson(t,TypeBean.class);

                        if (bean.getData() != null && bean.getData().size() >= 4){
                            if (bean.getData().get(0).getData() != null && bean.getData().get(0).getData().size() > 0){
                                typeList = bean.getData().get(0).getData();
                                adapter1.addList(bean.getData().get(0).getData());
                                mHomeTypeTitle.setText(typeList.get(typeIndex).getTitle());
                                adapter1.setSelectIndex(typeIndex);
                            }
                            if (bean.getData().get(1).getData() != null && bean.getData().get(1).getData().size() > 0){
                                addressList = bean.getData().get(1).getData();
                                adapter2.addList(bean.getData().get(1).getData());
                            }
                            if (bean.getData().get(2).getData() != null && bean.getData().get(2).getData().size() > 0){
                                yearList = bean.getData().get(2).getData();
                                adapter3.addList(bean.getData().get(2).getData());
                            }
                            if (bean.getData().get(3).getData() != null && bean.getData().get(3).getData().size() > 0){
                                lanuageList = bean.getData().get(3).getData();
                                adapter4.addList(bean.getData().get(3).getData());
                            }
                        }

                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {
                        initData();
                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }


    private void initData(){
        Log.e("TAG","typeList:"+typeList.get(typeIndex).getTitle());
        Log.e("TAG","addressList:"+addressList.get(addressIndex).getTitle());
        Log.e("TAG","lanuageList:"+lanuageList.get(lanuageIndex).getTitle());
        Log.e("TAG","yearList:"+yearList.get(yearIndex).getTitle());
        Log.e("TAG","index:"+index);
        if (vipIndex == 0){
            vipString = "";
        }else {
            vipString = "1";
        }
        Log.e("TAG","vipString:"+vipString);
        RequestUtil.getInstence().API().movieSearch(vipString,typeList.get(typeIndex).getId(),addressList.get(addressIndex).getTitle(),
                lanuageList.get(lanuageIndex).getTitle(),yearList.get(yearIndex).getTitle(),index+"",pid)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(this,true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        TwoTypeResBean bean = gson.fromJson(t,TwoTypeResBean.class);

                        adapter.clearList();
                        adapter.addList(bean.getData());
                    }

                    @Override
                    protected void onFail(int error_code) {
                        adapter.clearList();
                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }
}
